home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / microcrn / issue_46.arc / ISR.ARC / ISR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-11  |  3.0 KB  |  171 lines

  1. /*    ISR.c -  Routines to support Interrupt Service Routines.
  2.  
  3.         Supports ISR article in Micro Cornucopia Magazine Issue #46
  4. */
  5.  
  6.  
  7. #include <isr.h>
  8.  
  9.  
  10. #pragma vpindex isr_get_vector
  11. void far *isr_get_vector( inum )
  12. int inum;
  13. {
  14.     return (void far *)(*((long far *)((long)(inum * 4))));
  15. }
  16.  
  17.  
  18. #pragma vpindex isr_set_vector
  19. void isr_set_vector( inum, farfuncp )
  20. void far (*farfuncp)();
  21. int inum;
  22. {
  23. #asm
  24.     mov    ax,word ptr 4[bp]
  25.     shl    ax,1
  26.     shl    ax,1
  27.     mov     dx,0
  28.     mov    es,dx
  29.     mov    bx,ax
  30.     mov    dx,word ptr 8[bp]
  31.     mov    ax,word ptr 6[bp]
  32.     cli
  33.     mov    es:word ptr 2[bx],dx
  34.     mov     es:word ptr 0[bx],ax
  35.     sti
  36. #endasm
  37. }
  38.  
  39.  
  40. #pragma vpindex isr_install
  41. void isr_install( isr, isr_num, handler )
  42. isrh *isr;
  43. int isr_num;
  44. void far (*handler)();
  45. {
  46.     extern int _ISRHT_LENGTH;
  47.     extern unsigned char far *_ISRHT_PTR;
  48.     int i;
  49.  
  50.     setmem( isr, sizeof *isr, 0 );
  51.     /* the long form of movmem() is used here to avoid 
  52.        problems with the different memory models...    
  53.     */
  54.     for ( i = 0; i < _ISRHT_LENGTH; i++ )
  55.        ((char *)isr)[i] = _ISRHT_PTR[i];
  56.  
  57.     isr->hndlr = handler;
  58.     isr->isr_num = isr_num;
  59.     isr->isrho = ((long)((void far *)isr)) & 0xffff;
  60.     isr->isrhs = ((long)((void far *)isr)) >> 16;
  61.     isr->prev_hndlr = isr_get_vector( isr_num );
  62.  
  63.     if ( !isr->prev_hndlr )
  64.        isr->code_jmpf = OP_IRET;
  65.  
  66.     isr_set_vector( isr_num, (void far (*)())isr );
  67. }
  68.  
  69.  
  70. #pragma vpindex isr_restore
  71. void isr_restore( isr )
  72. isrh *isr;
  73. {
  74.     isr_set_vector( isr->isr_num, isr->prev_hndlr );
  75. }
  76.  
  77.  
  78. #ifdef MAIN
  79.  
  80. volatile long c;
  81. volatile int k;
  82.  
  83. __regs _REGS;
  84.  
  85.  
  86. main()
  87. {
  88.     isrh isr1, isr2, isr3, isrx;
  89.     void far int_1c(), int_09(), int_23();
  90.     int ascii;
  91.  
  92.     seg86( _REGSP );
  93.     __regs_dump( _REGSP );
  94.  
  95.     isr_install( &isr1, 0x1c, int_1c );
  96.     isr_install( &isr2, 0x09, int_09 );
  97.     isr_install( &isr3, 0x23, int_23 );
  98.  
  99.     printf("\nBet ya can't ^C, ^BREAK or CTRL-ALT-DEL out'a this one!\n");
  100.     printf("(hit ESC when you give up!)\n\n");
  101.  
  102.     ascii = 0;
  103.     do 
  104.     {  printf("Time: %lds, Last Scan code: %x    \r", c * 10l / 182l, k );
  105.        if ( constat() )
  106.           ascii = conin();
  107.     } while ( ascii != 27 );
  108.  
  109.     isr_restore( &isr1 );
  110.     isr_restore( &isr2 );
  111.     isr_restore( &isr3 );
  112. }
  113.  
  114.  
  115. int conin()
  116. {
  117.     pcdos( 0x08 );
  118.     return _AL;
  119. }
  120.  
  121.  
  122. int constat()
  123. {
  124.     pcdos( 0x0b );
  125.     return !!_AL;
  126. }
  127.  
  128.  
  129. void far int_09( isr, _REGS )
  130. isrh *isr;
  131. isr_regs _REGS;
  132. {
  133.     static int ctrl;
  134.  
  135.     k = inportb( 0x60 );
  136.  
  137.          if ( k == 29 )     /* control key pressed  */
  138.             ctrl = 1;
  139.     else if ( k == 29+0x80 )/* control key released */
  140.             ctrl = 0;
  141.     else if ( ctrl && ( k == 46 || k == 70 || k == 83 ) )
  142.          {  /* don't allow ^C, ^BREAK or ^DEL */
  143.             outportb( 0x61, inportb( 0x61 ) | 0x80 );
  144.             outportb( 0x61, inportb( 0x61 ) & 0x7f );
  145.             outportb( 0x20, 0x20 );
  146.             isr_iret( isr );
  147.             return;
  148.          };
  149.  
  150.     isr_pass( isr );
  151. }
  152.  
  153. void far int_1c( isr, _REGS )
  154. isrh *isr;
  155. isr_regs _REGS;
  156. {
  157.     c++;
  158.     isr_pass( isr );
  159. }
  160.  
  161. void far int_23( isr, _REGS )
  162. isrh *isr;
  163. isr_regs _REGS;
  164. {
  165.     _CARRY = 0;
  166.     isr_iret( isr );
  167. }
  168.  
  169. #endif
  170.  
  171.